package cn.fage.filters;

import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import java.io.IOException;
import java.util.Map;

/**
 * @author lin
 * @version 1.0
 * @date 2020-07-29 11:32
 * @Description TODO
 */

public class TwoFilter implements Filter {
    //初始化配置参数，在doFilter()方法之前被调用
    @Override
    public void init(FilterConfig config) throws ServletException {

    }


    //该方法在客户端请求及服务器端回复时都将被自动调用
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws ServletException, IOException {
        Map body = req.getParameterMap();
        System.out.println(" 进入到 TwoFilter ");
        if (body.containsKey("name")) {
            System.out.println("直接返回");
            String s = "直接返回 : null";
            String s1 = new String(s.getBytes(), "ISO8859-1");
            String s2 = new String(s.getBytes(), "UTF-8");
            resp.setCharacterEncoding("UTF-8");
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(JSON.toJSONString(s1));
            return;
        }
        chain.doFilter(req, resp);
        try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("servlet 执行完成后 再次 经过 过滤器 TwoFilter");
    }


    //结束过滤器，doFilter()方法完成后被调用
    @Override
    public void destroy() {
    }
}
